package com.dgut.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.dgut.common.SelectRole;
import com.dgut.common.StateForUser;
import com.dgut.common.StateOfNotice;
import com.dgut.pojo.Administrator;
import com.dgut.pojo.Buyer;
import com.dgut.pojo.ServiceProvider;
import com.dgut.pojo.User;
import com.dgut.pojo.receive.AdministratorAndBuyerReceive;
import com.dgut.pojo.receive.ServiceProvideReceive;
import com.dgut.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 存放无需登录即可访问的接口
 */
@CrossOrigin
@Api("IndexController")
@RestController
@RequestMapping("/index")
public class IndexController {

    @Resource
    private UserService userService;
    @Resource
    private ServiceProviderService serviceProviderService;
    @Resource
    private AdministratorService administratorService;
    @Resource
    private BuyerService buyerService;
    @Resource
    private NoticeService noticeService;
    /**
     * start-hwf
     */

    /**
     * 登录
     * @param user
     * @return
     */
    @ApiOperation(value = "登录接口",notes = "")
    @PostMapping("/login")
    public Object login(@ApiParam(value = "用户",required = true) @RequestBody User user){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();

        boolean pause = false; //账户状态不正常标识
        if(userService.check(user)){

            /**
             * 查看角色
             */
            Integer userId = user.getId();
            if(user.getRole().equals(SelectRole.BUYER)){
                Buyer buyer = buyerService.selectBuyerById(userId);
                //查看账户是否正常
                if(buyer.getState().equals(StateForUser.NORMAL)){
                    StpUtil.login(userId);
                    SaSession saSession = StpUtil.getSession();
                    saSession.set("role",SelectRole.BUYER);
                    saSession.set("user",buyer);
                    resultMap.put("user",buyer);
                    resultMap.put("noticeCount",noticeService.selectNoticeCount(userId, StateOfNotice.UNREAD));
                }else{
                    pause = true;
                }

            }else if(user.getRole().equals(SelectRole.SERVICE_PROVIDER)){
                ServiceProvider serviceProvider = serviceProviderService.selectServiceProviderById(userId);
                if(serviceProvider.getState().equals(StateForUser.NORMAL)){
                    StpUtil.login(userId);
                    SaSession saSession = StpUtil.getSession();
                    saSession.set("role",SelectRole.SERVICE_PROVIDER);
                    saSession.set("user",serviceProvider);
                    resultMap.put("user",serviceProvider);
                    resultMap.put("noticeCount",noticeService.selectNoticeCount(userId,StateOfNotice.UNREAD));
                }else{
                    pause = true;
                }

            }else if(user.getRole().equals(SelectRole.ADMIN)){
                Administrator administrator = administratorService.selectAdministratorById(userId);
                if(administrator.getState().equals(StateForUser.NORMAL)) {
                    StpUtil.login(userId);
                    SaSession saSession = StpUtil.getSession();
                    saSession.set("role", SelectRole.ADMIN);
                    saSession.set("user", administrator);
                    resultMap.put("user",administrator);
                    resultMap.put("noticeCount",noticeService.selectNoticeCount(userId,StateOfNotice.UNREAD));
                }else {
                    pause = true;
                }
            }else{
                StpUtil.login(userId);
                SaSession saSession = StpUtil.getSession();
                saSession.set("role", SelectRole.SUPER_ADMIN);
                saSession.set("user",user);
                resultMap.put("user",user);
            }
            if(!pause){
                resultMap.put("tokenInfo",StpUtil.getTokenInfo());
                resultMap.put("role",user.getRole());
                resultMap.put("result",true);
                resultMap.put("isLogin",true);
                resultMap.put("message","登录成功！");
            }else{
                resultMap.put("result",false);
                resultMap.put("message","登录失败！");
            }
        }else{
            resultMap.put("result",false);
            resultMap.put("isLogin",false);
            resultMap.put("message","账户或密码错误！");
        }
        return resultMap;
    }


    @ApiOperation(value = "采买方注册接口",notes = "")
    @PostMapping("/registerForBuyer")
    public Object registerForBuyer(@ApiParam(value = "采买方信息",required = true) @RequestBody AdministratorAndBuyerReceive administratorAndBuyerReceive) {
        ConcurrentHashMap<Object,Object> resultMap = new ConcurrentHashMap();
        if(buyerService.register(administratorAndBuyerReceive)){
            resultMap.put("result",true);
            resultMap.put("message","注册成功！");
        }else{
            resultMap.put("result",false);
            resultMap.put("message","注册失败！");
        }
        return resultMap;
    }

    /**
     * 服务商注册
     * @param serviceProvideReceive
     * @return
     */
    @ApiOperation(value = "服务商注册接口",notes = "")
    @PostMapping("/registerForServiceProvide")
    public Object registerForServiceProvide(@ApiParam(value = "服务商信息",required = true) @RequestBody ServiceProvideReceive serviceProvideReceive) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(serviceProviderService.register(serviceProvideReceive)){
            resultMap.put("result",true);
            resultMap.put("message","注册成功！");
        }else{
            resultMap.put("result",false);
            resultMap.put("message","注册失败！");
        }
        return resultMap;
    }

    @SaCheckLogin
    @ApiOperation(value = "退出登录",notes = "")
    @PostMapping("/logout")
    public void logout(){
        StpUtil.logout();
    }
    /**
     * end-hwf
     */
}
